package com.example.dell.fastshopping.user;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.user.ensureMoney.EnsureMoneyActivity;
import com.example.dell.fastshopping.loginRegist.LoginActivity;
import com.example.dell.fastshopping.order.OrderActivity;
import com.example.dell.fastshopping.trolley.TrolleyActivity;
import com.example.dell.fastshopping.user.UserInfo.UserinfoActivity;
import com.example.dell.fastshopping.user.bank.BankActivity;
import com.example.dell.fastshopping.user.entity.UserInfo;
import com.example.dell.fastshopping.user.UserInfo.UserViewModle;
import com.example.dell.fastshopping.user.setting.SettingActivity;
import com.example.dell.fastshopping.user.task.TaskActivity;
import com.example.dell.fastshopping.utils.GlideUtils;
import com.example.dell.fastshopping.utils.KLog;
import com.example.dell.fastshopping.utils.SPDao;
import com.example.dell.fastshopping.views.CommentItem;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ${YSH} on 2019/4/10.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */

public class UserFragment extends Fragment {


    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.jinpai_number)
    TextView jinpaiNumber;
    @BindView(R.id.user_info)
    LinearLayout userInfo;
    @BindView(R.id.my_money)
    TextView myMoney;
    @BindView(R.id.money)
    LinearLayout money;
    @BindView(R.id.my_integral)
    TextView myIntegral;
    @BindView(R.id.integral)
    LinearLayout integral;
    @BindView(R.id.my_ensure_money)
    TextView myEnsureMoney;
    @BindView(R.id.ensure_money)
    LinearLayout ensureMoney;
    @BindView(R.id.cardview)
    CardView cardview;
    @BindView(R.id.message)
    CommentItem message;
    @BindView(R.id.order)
    CommentItem order;
    @BindView(R.id.trolley)
    CommentItem trolley;
    @BindView(R.id.bank)
    CommentItem bank;
    @BindView(R.id.task)
    CommentItem task;
    @BindView(R.id.mysold)
    CommentItem mysold;
    @BindView(R.id.setting)
    CommentItem setting;
    @BindView(R.id.cardview2)
    CardView cardview2;
    @BindView(R.id.haslogin)
    LinearLayout haslogin;
    @BindView(R.id.un_login)
    TextView unLogin;
    @BindView(R.id.user_icon)
    ImageView userIcon;
    @BindView(R.id.message_num)
    TextView message_num;
    private Unbinder unbinder;
    private Context mContext;
    private UserViewModle userViewModle;
    private UserInfo mUserInfo;

    public static UserFragment newInstance() {
        return new UserFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_user, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        userViewModle = ViewModelProviders.of(this).get(UserViewModle.class);
    }

    @Override
    public void onResume() {
        super.onResume();
        intView();
    }

    private void intView() {
        KLog.e("刷新");
        boolean shouldLogin = SPDao.getShouldLogin();
        if (shouldLogin) {
            message_num.setVisibility(View.GONE);
            haslogin.setVisibility(View.GONE);
            unLogin.setVisibility(View.VISIBLE);
            myMoney.setText("￥" + 0);
            myIntegral.setText("0");
            myEnsureMoney.setText("￥" + 0);
            userIcon.setImageResource(R.mipmap.default_user_icon);
        } else {
            userViewModle.getUserInfo().observe(this, new Observer<UserInfo>() {
                @Override
                public void onChanged(@Nullable UserInfo userInfo) {
                    mUserInfo = userInfo;
                    haslogin.setVisibility(View.VISIBLE);
                    unLogin.setVisibility(View.GONE);
                    userName.setText(userInfo.getUserName());
                    jinpaiNumber.setText("竞拍号" + userInfo.getId());
                    myMoney.setText("￥" + userInfo.getBalanceString());
                    myIntegral.setText(userInfo.getIntegral() + "");
                    String amount = "￥" + userInfo.getIntegralString();
                    myEnsureMoney.setText(amount);
                    GlideUtils.setRoundView(mContext, userInfo.getIcon(), userIcon);
                    //保存竞拍场 我交过的保证金，首页会进行权限校验
                    List<UserInfo.Amounts> amountsList = userInfo.getList();
                    KLog.e("222222222222222222222");
                    SPDao.saveAmounts(amountsList);
                    SPDao.saveUserPhone(userInfo.getPhone());
                }
            });


            int num = SPDao.getUnReadMessageNum();

            if (num > 0) {
                message_num.setVisibility(View.VISIBLE);
            } else {
                message_num.setVisibility(View.GONE);
            }

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.bank, R.id.trolley, R.id.message, R.id.order, R.id.task, R.id.setting,
            R.id.integral, R.id.ensure_money, R.id.money, R.id.user_info, R.id._setting})
    public void onViewClicked(View view) {
        boolean shouldLogin = SPDao.getShouldLogin();
        if (shouldLogin) {
            goLogin();
            return;
        }
        Intent intent;
        switch (view.getId()) {
            case R.id.message:
                intent = new Intent(mContext, MessageActivity.class);
                startActivity(intent);
                break;
            case R.id.order:
                intent = new Intent(mContext, OrderActivity.class);

                startActivity(intent);
                break;
            case R.id.task:
                intent = new Intent(mContext, TaskActivity.class);
                startActivity(intent);
                break;
            case R.id._setting:
            case R.id.setting:
                intent = new Intent(mContext, SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.integral:
                intent = new Intent(mContext, IntegralActivity.class);
                startActivity(intent);
                break;
            case R.id.ensure_money:
                intent = new Intent(mContext, EnsureMoneyActivity.class);
                startActivity(intent);
                break;
            case R.id.money://余额
                intent = new Intent(mContext, MoneyActivity.class);
                intent.putExtra("balance", mUserInfo.getBalance());
                startActivity(intent);
                break;
            case R.id.trolley:
                intent = new Intent(mContext, TrolleyActivity.class);
                startActivity(intent);
                break;
            case R.id.bank:
                intent = new Intent(mContext, BankActivity.class);
                startActivity(intent);
                break;
            case R.id.user_info:
                goUserBaseInfo();
                break;
        }
    }

    private void goUserBaseInfo() {
        Intent intent = new Intent(mContext, UserinfoActivity.class);
        startActivity(intent);
    }

    private void goLogin() {
        Intent intent = new Intent(mContext, LoginActivity.class);
        startActivity(intent);
    }

}
